package com.example.questionnaires.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionnaires.common.Result;
import com.example.questionnaires.entity.Announcement;
import com.example.questionnaires.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.example.questionnaires.common.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import com.example.questionnaires.common.BusinessException;
import com.example.questionnaires.dto.AnnouncementDTO;
import com.example.questionnaires.service.AnnouncementCommentService;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    private static final Logger log = LoggerFactory.getLogger(AnnouncementController.class);

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private AnnouncementCommentService commentService;

    @GetMapping("/list")
    public Result<PageResult<Announcement>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            return announcementService.list(page, size);
        } catch (Exception e) {
            log.error("获取公告列表失败", e);
            return Result.error("获取公告列表失败");
        }
    }

    @GetMapping("/{id}")
    public Result<AnnouncementDTO> getAnnouncementById(@PathVariable Long id) {
        Announcement announcement = announcementService.getById(id);
        if (announcement == null) {
            throw new BusinessException("公告不存在");
        }
        
        AnnouncementDTO dto = new AnnouncementDTO();
        BeanUtils.copyProperties(announcement, dto);
        dto.setCommentCount(commentService.getCommentsByAnnouncementId(id).size());
        return Result.success(dto);
    }

    @GetMapping("/latest")
    public Result<List<Announcement>> getLatest() {
        try {
            List<Announcement> announcements = announcementService.getLatest();
            return Result.success(announcements);
        } catch (Exception e) {
            log.error("获取最新公告失败", e);
            return Result.error("获取最新公告失败");
        }
    }

    @PostMapping
    public Result<?> save(@RequestBody Announcement announcement) {
        try {
            announcementService.save(announcement);
            return Result.success("发布成功");
        } catch (Exception e) {
            log.error("发布公告失败", e);
            return Result.error("发布公告失败");
        }
    }

    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Long id, @RequestBody Announcement announcement) {
        try {
            announcement.setId(id);
            announcementService.updateById(announcement);
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新公告失败", e);
            return Result.error("更新公告失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        try {
            announcementService.removeById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除公告失败", e);
            return Result.error("删除公告失败");
        }
    }
} 